"""
员工模型
"""
from sqlalchemy import Column, String, Integer, Enum, Numeric, ForeignKey, DateTime, func
from sqlalchemy.orm import relationship
from .base import Base


class Employee(Base):
    """员工表"""
    __tablename__ = "employees"
    
    employee_no = Column(String(20), unique=True, nullable=False, comment="员工编号")
    name = Column(String(50), nullable=False, comment="员工姓名")
    department_id = Column(Integer, ForeignKey("departments.id"), nullable=False, comment="部门ID")
    position = Column(String(50), comment="职位")
    phone = Column(String(20), comment="手机号")
    email = Column(String(100), comment="邮箱")
    password_hash = Column(String(255), comment="密码哈希")
    status = Column(Enum("active", "inactive", name="employee_status"), 
                   default="active", comment="状态")
    rpl_efficiency = Column(Numeric(5, 2), default=0.00, comment="RPL效率值（售前部门专用）")
    
    # 关系
    department = relationship("Department", back_populates="employees")
    team_assignments = relationship("EmployeeTeam", back_populates="employee")
    led_teams = relationship("Team", back_populates="leader")
    
    # 订单相关关系
    sales_orders = relationship("Order", foreign_keys="Order.sales_employee_id", back_populates="sales_employee")
    reception_orders = relationship("Order", foreign_keys="Order.reception_employee_id", back_populates="reception_employee")
    customer_service_orders = relationship("Order", foreign_keys="Order.customer_service_employee_id", back_populates="customer_service_employee")
    
    # 跟进记录
    follow_ups = relationship("StudentFollowUp", back_populates="employee")
    
    # 课程分配
    class_assignments = relationship("StudentCourseAssignment", back_populates="class_teacher")
    teacher_assignments = relationship("TeacherStageAssignment", back_populates="teacher")
    course_schedules = relationship("CourseSchedule", back_populates="teacher")
    
    # 业绩统计
    sales_performance = relationship("SalesPerformance", back_populates="employee")
    traffic_allocations = relationship("TrafficAllocation", back_populates="sales_employee")
    traffic_statistics = relationship("TrafficStatistics", foreign_keys="TrafficStatistics.reception_employee_id", back_populates="reception_employee")
    
    # 退款审批
    refund_approvals = relationship("RefundRecord", back_populates="approver")
    
    # 系统日志
    system_logs = relationship("SystemLog", back_populates="user")
    
    def __repr__(self):
        return f"<Employee(id={self.id}, name='{self.name}', employee_no='{self.employee_no}')>"
